package com.example.f22015303.test;

import com.example.f22015303.trans.Usertrans;
import com.example.f22015303.domian.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
//import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.beans.factory.annotation.Autowired;
//import org.junit.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@SpringBootTest
public class maintest {
    @Autowired
    InputStream inputStream;
    SqlSessionFactoryBuilder builder;
    SqlSessionFactory factory;
    SqlSession session;
    Usertrans usertrans;

    //在执行@Test内容前会先执行@Before的内容
    @BeforeEach
    public void init() throws IOException {
        inputStream= Resources.getResourceAsStream("config.xml");
        builder=new SqlSessionFactoryBuilder();
        factory=builder.build(inputStream);
        session=factory.openSession();
        usertrans=session.getMapper(Usertrans.class);
    }

    @Test
    public void test01FindAll(){
        List<User> users=usertrans.findAll();
        for (User user:users){
            System.out.println(user);
        }
    }

    @Test
    public void test02FindById(){
        User user=usertrans.findById(41);
        System.out.println(user);
    }

    @Test
    public void test03SaveUser(){
        User user=new User();
        user.setAddress("**省**市***街道**号");
        user.setUsername("李");
        user.setSex("男");
        user.setBirthday(new Date());
        usertrans.saveUser(user);
        session.commit();
        System.out.println(user);
    }
    @Test
    public void test04UpdateUser(){
        User user=usertrans.findById(41);
        user.setUsername("le");
        usertrans.updateUser(user);
        session.commit();
    }
    @Test
    public void test05DeleteById(){
        usertrans.deleteById(42);
        session.commit();
    }

    @Test
    public void test06FindByUserName(){
        List<User> users=usertrans.findByName("%二%");
        for(User user:users){
            System.out.println(user);
        }
    }
    //在执行@Test内容前会先执行@Before的内容
    @AfterEach
    public void destory() throws IOException {
        session.close();
        inputStream.close();
    }
}
